package com.stuManSystem.util;




import com.stuManSystem.config.AppConstants;

import java.sql.*;
/*
 *1. 连接数据库
 *2. 关闭数据库
 *3. 增删改查的底层操作
 *
 * */
public class DBUtil {



    private static DBUtil db;
    //jdbc连接数据库的钥匙
    private Connection conn;
    //sql语句的操作句柄
    private PreparedStatement ps;
    //查询的结果集
    private ResultSet rs;

    private DBUtil() {

    }

    //单例模式，设计模式的一种，只允许当前类存在一个对象
    public static DBUtil getDBUtil() {
        if (db == null) {
            db = new DBUtil();
        }
        return db;
    }

    //select * from table
    public int executeUpdate(String sql) {
        //增删改操作，如果操作成功，返回操作数据的数量。
        int result = -1;
        if (getConn() == null) {
            return result;
        }
        try {
            ps = conn.prepareStatement(sql);
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    //select * from table where name = ? and password = ?   ['张三','123']
    public int executeUpdate(String sql, Object[] obj) {
        //增删改的操作数
        int result = -1;
        if (getConn() == null) {
            return result;
        }
        try {
            ps = conn.prepareStatement(sql);

            for (int i = 0; i < obj.length; i++) {
                //setObject参数的索引从1开始
                ps.setObject(i + 1, obj[i]);
            }

            result = ps.executeUpdate();
            close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public ResultSet executeQuery(String sql) {
        if (getConn() == null) {
            return null;
        }
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    public ResultSet executeQuery(String sql, Object[] obj) {
        if (getConn() == null) {
            return null;
        }
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                ps.setObject(i + 1, obj[i]);
            }
            rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return rs;
    }

    //连接，实例化conn
    //1..java转为.class
    //2..class扔进虚拟机，加载类，静态加载和动态加载
    private Connection getConn() {
        try {
            if (conn == null || conn.isClosed()) {
                //动态的去加载一个类，
                Class.forName(AppConstants.JDBC_DRIVER);
                //实例化连接符对象
                conn = DriverManager.getConnection(AppConstants.JDBC_URL, AppConstants.JDBC_USERNAME,
                        AppConstants.JDBC_PASSWORD);
            }
        } catch (ClassNotFoundException e) {
            System.out.println("jdbc driver is not found.");
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public void close() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
